package com.ficus.road.maintain.controller;

import com.ficus.road.maintain.core.model.AlertRequest;
import com.ficus.road.maintain.dto.MonitorResponse;
import com.ficus.road.maintain.process.LoadSsfFileProcessor;
import com.ficus.road.maintain.service.MonitorService;
import com.ficus.road.maintain.service.VideoService;
import com.ficus.road.maintain.service.carmanager.CarManagerService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author xiawen yang
 * @date 2021/11/15 下午4:56
 */
@RestController
@RequiredArgsConstructor
@Slf4j
public class MonitorController {

    private final MonitorService monitorService;
    private final VideoService videoService;

    @GetMapping("/monitor/{date}")
    public ResponseEntity<MonitorResponse> monitor(@PathVariable("date") String date) {
        MonitorResponse monitorResponse = monitorService.monitor(date);
        return new ResponseEntity<>(monitorResponse, HttpStatus.OK);
    }

    @GetMapping("/monitor/alertType/query")
    public ResponseEntity<Map<String, String>> queryAlertType() {
        Map<String, String> alertType = monitorService.getAlertType();
        return new ResponseEntity<>(alertType, HttpStatus.OK);
    }

    @PostMapping("/monitor/prometheus_alert")
    public ResponseEntity<String> processAlert(@RequestBody AlertRequest prometheusAlert) {
        monitorService.processAlert(prometheusAlert);
        log.info("开始处理prometheus告警信息");
        return new ResponseEntity<>("success", HttpStatus.OK);
    }

    @PutMapping("/monitor/carMonitor")
    public ResponseEntity<String> carMonitor() {
        monitorService.carMonitor();
        return new ResponseEntity<>("success", HttpStatus.OK);
    }

    @PutMapping("/monitor/businessMonitor")
    public ResponseEntity<String> businessMonitor() {
        monitorService.businessMonitor();
        return new ResponseEntity<>("success", HttpStatus.OK);
    }

    @PutMapping("/monitor/eventMonitor")
    public ResponseEntity<String> eventMonitor() {
        monitorService.eventMonitor();
        return new ResponseEntity<>("success", HttpStatus.OK);
    }

    @PutMapping("/monitor/statisticOnStatistic")
    public ResponseEntity<String> statisticOnStatistic() {
        monitorService.statisticOnStatistic();
        return new ResponseEntity<>("success", HttpStatus.OK);
    }

    @GetMapping("/monitor/exportExcel")
    public ResponseEntity<String> export2Excel() {
        String res = monitorService.exportExcel();
        return new ResponseEntity<>(res, HttpStatus.OK);
    }

    @GetMapping("/monitor/reset")
    public ResponseEntity<String> reset() {
        monitorService.reset();
        return new ResponseEntity<>("success", HttpStatus.OK);
    }

    @GetMapping("/monitor/dingTalk")
    public ResponseEntity<String> dingTalk() {
        monitorService.dingTalk();
        return new ResponseEntity<>("success", HttpStatus.OK);
    }

    @GetMapping("/monitor/timingCleanup")
    public ResponseEntity<String> timingCleanup() {
        monitorService.timingCleanup();
        return new ResponseEntity<>("success", HttpStatus.OK);
    }

    @RequestMapping(value = "/monitor/video/printFlag")
    public ResponseEntity<List<Object>> printFlag() {
        List<Object> res = new ArrayList<>();
        res.add("playFlag:");
        res.add(LoadSsfFileProcessor.getPlayFlag().get());
        res.add("liveCarSet:");
        res.add(LoadSsfFileProcessor.getLiveCarSet().get());
        return ResponseEntity.ok(res);
    }

    @RequestMapping(value = "/monitor/carManager")
    public ResponseEntity<List<Object>> printCarUpAndDownMap() {
        List<Object> res = new ArrayList<>();
        res.add("carUpMap:");
        res.add(CarManagerService.CAR_UP_MAP);
        res.add("carDownMap:");
        res.add(CarManagerService.CAR_DOWN_MAP);
        return ResponseEntity.ok(res);
    }
}
